package com.Txunda.fxw.ui.fgt;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.other.LeaveMessageAty;
import com.Txunda.fxw.ui.activity.other.NoticeAty;
import com.Txunda.fxw.ui.adapter.MyRecyclerAdapter;
import com.Txunda.fxw.ui.base.BaseFgt;
import com.Txunda.fxw.ui.activity.carve.CarveListAty;
import com.Txunda.fxw.ui.http.AboutUs;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.http.Syb;
import com.Txunda.fxw.ui.adapter.model.LawDistrict;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BusinessFgt extends BaseFgt {
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;

    @ViewInject(R.id.aty_title3_tv_right2)
    private TextView aty_title3_tv_right2;

    private XCSlideView mSlideViewRight;


    private RecyclerView main_recycler;
    private MyRecyclerAdapter adapter;
    private ListView law_area_lv;
    private TextView tv_weibo;
    private TextView tv_link;

    @ViewInject(R.id.carve_court_case)
    private ImageView carve_court_case;

    @ViewInject(R.id.aty_title3_icon)
    private ImageView aty_title3_icon;

    private Office office;
    private ArrayList<Map<String, String>> list_data;
    private ArrayList<Map<String, String>> recycler_list_data;
    private AboutUs aboutUs;

    private ArrayList<LawDistrict> law_district_list;
    private MyAdapter law_firm_adapter;

    private String district_name;
    private ArrayList<LawDistrict> cityList;
    private ArrayList<LawDistrict> districtList;

    Map<String, String> map2;
    private Syb syb;
    private String city_id;

    protected int index;

    public BusinessFgt() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        removeProgressContent();
        removeProgressDialog();
        // haveData = false;
        district_name = cityList.get(index).getProvince_name(); // 地区名称
        showToast("已切换至" + district_name);
        aty_title3_tv_right2.setText(district_name);

        if (mSlideViewRight.isShow()) {
            mSlideViewRight.dismiss();
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_business;
    }

    @Override
    @OnClick({R.id.carve_court_case, R.id.carve_legal_advice, R.id.carve_firm, R.id.img_logo})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.img_logo: // logo
                if (!mSlideViewRight.isShow())
                    mSlideViewRight.show();
                aboutUs.introduce(this);
                break;
            case R.id.carve_court_case: // 汽车租赁平台
                Log.d("zdl", "========== "+city_id);
                Log.d("zdl", "========== "+district_name);
                Bundle bundle = new Bundle();
                bundle.putString("from", "1");
                bundle.putString("name", "汽车租赁平台");
                bundle.putString("city_id", city_id);
                bundle.putString("district_name", district_name);
                startActivity(CarveListAty.class, bundle);
                break;
            case R.id.carve_legal_advice: // 投资人
                bundle = new Bundle();
                bundle.putString("from", "2");
                bundle.putString("name", "投资人");
                bundle.putString("city_id", city_id);
                bundle.putString("district_name", district_name);
                startActivity(CarveListAty.class, bundle);
                break;
            case R.id.carve_firm: // 融资人
                bundle = new Bundle();
                bundle.putString("from", "3");
                bundle.putString("name", "融资人");
                bundle.putString("city_id", city_id);
                bundle.putString("district_name", district_name);
                startActivity(CarveListAty.class, bundle);
                break;
            default:
                break;
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("introduce")) { // 介绍信息
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if (MapUtils.isEmpty(map))
                return;
            if ("success".equals(map.get("flag"))) {
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                if (MapUtils.isEmpty(data))
                    return;
                String microblog = data.get("microblog"); // 微博
                final String url = data.get("url");
                recycler_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
                tv_weibo.setText(microblog);

                tv_link.setText(url);
                adapter.setList(recycler_list_data);
                main_recycler.setAdapter(adapter);

                tv_link.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);

                    }
                });
            }
        }
        if (requestUrl.contains("Syb/index")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if (MapUtils.isEmpty(map))
                return;
            if ("success".equals(map.get("flag"))) {
                list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                if (ListUtils.isEmpty(list_data))
                    return;
                for (Map<String, String> temp : list_data) {
                    switch (Integer.parseInt(temp.get("position"))) {
                        case 1:
                            Glide.with(this).load(temp.get("pic")).into(carve_court_case);
                            break;
                        case 2:
//                            Glide.with(this).load(temp.get("pic")).into(carve_legal_advice);
                            break;
                        case 3:
//                            Glide.with(this).load(temp.get("pic")).into(carve_firm);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        if (requestUrl.contains("areaList")) { // 律师事务所地区选择
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if (MapUtils.isEmpty(map))
                return;
            if ("success".equals(map.get("flag"))) {
                law_district_list.clear();
                List<Map<String, String>> tempList = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                if (ListUtils.isEmpty(tempList))
                    return;
                for (Map<String, String> temp : tempList) {
                    law_district_list.add(new LawDistrict(temp.get("o_id"), temp.get("province_id"), temp.get("province_name")));
                }

                law_firm_adapter = new MyAdapter(law_district_list);
                law_area_lv.setAdapter(law_firm_adapter);

                law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        law_district_list.get(position).getO_id();
                        String province_id = law_district_list.get(position).getProvince_id(); // 省级id
                        showProgressDialog();
                        office.areaCity(province_id, BusinessFgt.this);
                        law_firm_adapter.notifyDataSetChanged();
                        law_district_list.clear();
                    }
                });
            }
        }
        if (requestUrl.contains("areaCity")) { // 市级

            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                cityList.clear();
                for (Map<String, String> temp : JSONUtils.parseKeyAndValueToMapList(map.get("data"))) {
                    cityList.add(new LawDistrict(temp.get("o_id"), temp.get("city_id"), temp.get("city_name")));
                }
                law_firm_adapter = new MyAdapter(cityList);
                law_area_lv.setAdapter(law_firm_adapter);

                law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 城市id
                        city_id = cityList.get(position).getProvince_id();
                        showProgressDialog();
                        office.areaDistrict(city_id, BusinessFgt.this);
                        index = position;

                        law_firm_adapter.notifyDataSetChanged();
                    }
                });
            }

        }
        if (requestUrl.contains("areaDistrict")) {
            map2 = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map2.get("flag"))) {
                districtList.clear();
                for (Map<String, String> temp : JSONUtils.parseKeyAndValueToMapList(map2.get("data"))) {
                    districtList
                            .add(new LawDistrict(temp.get("o_id"), temp.get("district_id"), temp.get("district_name")));
                }
                law_firm_adapter = new MyAdapter(districtList);

                law_area_lv.setAdapter(law_firm_adapter);

                law_area_lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        // 地区id
                        district_name = districtList.get(position).getProvince_name(); // 地区名称
                        showToast("已切换至" + district_name);
                        aty_title3_tv_right2.setText(district_name);
                        law_firm_adapter.notifyDataSetChanged();

                        if (mSlideViewRight.isShow()) {
                            mSlideViewRight.dismiss();
                        }
                    }
                });
            }
        }
    }

    @Override
    protected void initialized() {
        cityList = new ArrayList<>();
        law_district_list = new ArrayList<>();
        districtList = new ArrayList<>();
        aboutUs = new AboutUs();
        recycler_list_data = new ArrayList<>();
        list_data = new ArrayList<>();
        office = new Office();


        mContext = getActivity();
        // 屏幕宽度
        int mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(getActivity(), XCSlideView.Positon.RIGHT);
        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_slideview, null);
        TextView tv_notice = menuViewRight.findViewById(R.id.tv_notice);
        TextView tv_comment = menuViewRight.findViewById(R.id.tv_comment);
        mSlideViewRight.setMenuView(getActivity(), menuViewRight);
        main_recycler = menuViewRight.findViewById(R.id.main_recycler);
        tv_link = menuViewRight.findViewById(R.id.tv_link);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);
        tv_weibo = menuViewRight.findViewById(R.id.tv_weibo);
        // 公告
        tv_notice.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(NoticeAty.class, null);
            }
        });

        tv_weibo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showTips(R.drawable.error, "敬请期待");
            }
        });
        tv_comment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(LeaveMessageAty.class, null);
            }
        });
        syb = new Syb();
    }

    @Override
    protected void requestData() {
        String area_name = PreferenceUtil.readString(getActivity().getApplicationContext(), PreferenceUtil.sp_mode);
        aty_title3_tv_center.setText("创业平台");
        aty_title3_tv_right2.setVisibility(View.VISIBLE);
        aty_title3_tv_right2.setText(area_name);
        showProgressDialog();

        office.lawList(this);
        syb.index(this);
        aty_title3_icon.setVisibility(View.GONE);
    }

    /**
     * 律师地区筛选adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {
        List<LawDistrict> list;
        MyViewHolder vh;

        public MyAdapter(List<LawDistrict> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (list != null) {
                ret = list.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.law_item_slide, null);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            vh.item_slide_law.setText(list.get(position).getProvince_name()); // 地区名称

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_slide_law)
            private TextView item_slide_law;

        }

    }


}
